﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Messenger.DataAccess.Attributes
{
    /// <summary>
    /// Text range validation
    /// Created by: Dinh Quoc Vuong
    /// Created date: Oct 6th 2011
    /// </summary>
    public class TextRangeValidationAttribute : ValidationBaseAttribute
    {
        #region Properties
        /// <summary>
        /// Minimum characters length
        /// </summary>
        public int MinLength { get; set; }
        /// <summary>
        /// Maximum characters length
        /// </summary>
        public int MaxLength { get; set; }
        #endregion

        #region constuctors
        public TextRangeValidationAttribute()
            : base()
        {
            //set default number
            MinLength = int.MinValue;
            //set default number
            MaxLength = int.MinValue;
        }
        #endregion

        #region overrides
        /// <summary>
        /// Validate text length
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Validate(object obj)
        {
            bool isValid = false;
            string value = (string)obj;
            isValid = !string.IsNullOrEmpty(value);

            if (isValid)
            {
                int length = value.Length;
                if (MinLength != int.MinValue && MaxLength != int.MinValue)
                {
                    isValid = length >= MinLength && length <= MaxLength;
                }
                else if (MinLength != int.MinValue)
                {
                    isValid = length >= MinLength;
                }
                else if (MaxLength != int.MinValue)
                {
                    isValid = length <= MaxLength;
                }
            }
            return isValid;
        }

        /// <summary>
        /// Get error message
        /// </summary>
        /// <param name="propertyName">your property name</param>
        /// <returns></returns>
        public override string GetErrorMessage(string propertyName = null)
        {
            string msg = Message;
            if (msg == null)
            {
                msg = "Property " + propertyName + " is not valid text range. ";
                if (MinLength != int.MinValue && MaxLength != int.MinValue)
                {
                    msg += string.Format("Length must be in range ({0} - {1}).", MinLength, MaxLength);
                }
                else if (MinLength != int.MinValue)
                {
                    msg += string.Format("Length must be at least {0} characters.", MinLength);
                }
                else if (MaxLength != int.MinValue)
                {
                    msg += string.Format("Length must be less than {0} characters.", MaxLength);
                }
            }
            return msg;
        }
        #endregion
    }
}
